package org.daidai.abstrusewriting.homepage.hot.fragments;


import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import org.daidai.abstrusewriting.R;
import org.daidai.abstrusewriting.homepage.hot.activity.EditRecommendActivity;
import org.daidai.abstrusewriting.homepage.hot.adapters.MyRecsAdapter;
import org.daidai.abstrusewriting.homepage.hot.client.ClientApi;
import org.daidai.abstrusewriting.homepage.hot.model.RecommendDetail;
import org.daidai.abstrusewriting.homepage.hot.model.RecommendItemDetail;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class DetailFragment extends BaseFragment implements AdapterView.OnItemClickListener {


    private TextView mDetailcontent;
    private TextView mName;
    private TextView mFocus;
    private ImageView mFace;
    private ListView mListView;
    private MyRecsAdapter mAdapter;
    private List<RecommendItemDetail> mList;
    private RecommendDetail recommendDetail;
    public DetailFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getDatilRecommend(RecommendDetail recommendDetail){
        mDetailcontent.setText(recommendDetail.getDetail().getIntro());
        mName.setText(recommendDetail.getUser().getNickname());
        mFocus.setText(recommendDetail.getUser().getFollowers());
        Picasso.with(getContext())
                .load(recommendDetail.getUser().getSmallLogo())
                .config(Bitmap.Config.ARGB_8888)
                .into(mFace);
        List<RecommendItemDetail> list = recommendDetail.getRecs().getItemDetailList();
        mList.clear();
        mList.addAll(list);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View ret = null;

        ret=inflater.inflate(R.layout.fragment_detail, container, false);
        mDetailcontent = ((TextView) ret.findViewById(R.id.fragment_detail_content));
        mName = ((TextView) ret.findViewById(R.id.fragment_detail_name));
        mFocus = ((TextView) ret.findViewById(R.id.fragment_detail_focus));
        mFace = ((ImageView) ret.findViewById(R.id.fragment_detail_face));
        mListView = ((ListView) ret.findViewById(R.id.fragment_recs_listview));
        mList = new ArrayList<>();
        mAdapter = new MyRecsAdapter(getContext(),mList);
        mListView.setAdapter(mAdapter);
        mListView.setOnItemClickListener(this);
        return ret;
    }

    @Override
    public void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    @Override
    public String getTitle() {
        return "详情";
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        RecommendItemDetail recommendItemDetail = mList.get(position);
        Intent intent = new Intent(getContext(),EditRecommendActivity.class);
        intent.putExtra("data",recommendItemDetail);
        startActivity(intent);
    }
}
